package org.myspringframework.springboot001.component;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author fzy
 * @version 1.0
 * 创建时间：2024-11-26 15:00
 */

@Aspect
@Component
public class LogAspect {

    @Before("execution(* org.myspringframework.springboot001.service..*(..))")
    public void log() {
        System.out.println("Before 执行");
    }

    @After("execution(* org.myspringframework.springboot001.service..*(..))")
    public void log2() {
        System.out.println("After 执行");
    }

    @AfterReturning("execution(* org.myspringframework.springboot001.service..*(..))")
    public void log3() {
        System.out.println("AfterReturning 执行");
    }

    @Around("execution(* org.myspringframework.springboot001.service..*(..))")
    public Object log4(ProceedingJoinPoint joinPoint) throws Throwable {
        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();    // 获取方法名
        Object[] args = joinPoint.getArgs();        // 获取参数列表

        System.out.println("Around 执行前");
        System.out.println(methodName + "执行了。。。参数是：" + Arrays.toString(args));
        Object result = joinPoint.proceed();        // 执行方法
        System.out.println("Around 执行后");

        return result;
    }
}
